package cn.trda.service.impl;

import java.util.List;

import cn.trda.mapper.AsContactsMapper;
import cn.trda.mapper.AsCustomsMapper;
import cn.trda.pojo.Contacts;
import cn.trda.pojo.Customs;
import cn.trda.pojo.User;
import cn.trda.service.ContactService;
import cn.trda.service.CustomerService;

import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	AsCustomsMapper customerMapper;
	@Autowired
	ContactService contactService;
	
	@Override
	public List<Customs> getCustomerList(Customs customer,Integer page,Integer rows) {
		if(page!=null&&rows!=null){
			return customerMapper.selectCustomerList(customer.getAgentid(),customer.getCustomname(),(page-1)*rows,rows);	
		}else{
			return customerMapper.selectCustomerList(customer.getAgentid(),customer.getCustomname(),null,null);
		}
	}

	@Override
	public Customs getCustomerById(Long id) {
		// TODO Auto-generated method stub
		return customerMapper.selectByPrimaryKey(id);
	}

	@Override
	public Integer getCustomerCount(Customs customer) {
		// TODO Auto-generated method stub
		return customerMapper.selectCount(customer.getAgentid(),customer.getCustomname());
	}

	@Override
	public boolean addCustomer(Customs customer) {
		// TODO Auto-generated method stub
		customerMapper.insert(customer);
		if(customer.getId()!=null){
			contactService.addContacts(customer);			
		}
		return customer.getId()!=null;
	}

	@Override
	public boolean updateCustomer(Customs customer) {
		// TODO Auto-generated method stub
		//更改联系人表
		contactService.addContacts(customer);
		//更改客户表
		return customerMapper.updateByPrimaryKey(customer)>0;
	}

	@Override
	public boolean isNameExist(String name) {
		// TODO Auto-generated method stub
		return customerMapper.selectByName(name)!=null;
	}





	
	

}
